<?php
/**
 * 后台用户管理模块
 *
 */
class StaticAction extends AdminAction {
	public function _initialize() {
		parent::_initialize (); // RBAC 验证接口初始化
	}
	private $root_name = "";
	private $site_root = '';
	private $file_noexist = 'file_noexist';
	private $dele_statice_success = 'dele_statice_success';
	private $dele_statice_fail = 'dele_statice_fail';
	private $delete_success = 'delete_success';
	private $delete_fail = 'delete_fail';
	private $statice_success = 'statice_success';
	private $statice_fail = 'staice_fail';
	private $pro_no_statice_name = 'pro_no_statice_name';
	private $art_no_statice_name = 'art_no_statice_name';
	private $howto_no_statice_name = 'howto_no_statice_name';
	private $plate_no_statice_name = 'plate_no_statice_name';
	public function __construct() {
		$this->root_name = 'http://' . $_SERVER ['HTTP_HOST'] . '/index.php';
		$this->site_root = 'http://' . $_SERVER ['HTTP_HOST'];
		parent::__construct ();
	}

	// 静态化全部一级大类页
	public function staticeAllBig() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$bigcategory = M ( 'Bigcategory' );
		$data = $bigcategory->select ();
		foreach ( $data as $v ) {
			$id = $v ['id'];
			if (! $id) {
				return;
			}
			if (empty ( $v ['big_category_name'] )) {
				continue;
			}
			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/product/id/' . $id );
			if (! file_put_contents ( "./" . strtolower ( $v ['big_category_name'] ) . '-software.html', $p )) {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}

	// 静态化全部文章页
	public function staticAllArticle() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$article = M ( 'Article' );
		if ($this->isPost ()) {
			$start_id = isset ( $_POST ['start_id'] ) ? intval ( $_POST ['start_id'] ) : 1;
			$end_id = isset ( $_POST ['end_id'] ) ? intval ( $_POST ['end_id'] ) : 1;
			$allArt = $article->where ( 'statice_name != "" and id>=' . $start_id . " and id<=" . $end_id )->select ();

			foreach ( $allArt as $v ) {
				$id = $v ['id'];
				if (! $id) {
					return;
				}

				if (! $v ['statice_name']) {
					continue;
				}

				$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/article/id/' . $id );
				if (file_put_contents ( $v ['statice_name'] . '.html', $p )) {
					$article->where ( 'id=' . $id )->save ( array (
							'is_statice' => '1'
					) );
				} else {
					echo "<script>alert('静态化失败');history.back();</script>";
				}
			}
			echo "<script>alert('静态化成功');location.href='" . __URL__ . "/staticAllArticle';</script>";
		} else {
			$id = $article->field ( "id" )->limit ( 1 )->order ( 'id desc' )->find ();
			$this->assign ( "id", $id );
			$this->display ();
		}
	}

	// 静态化全部产品页
	public function staticAllProduct() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$product = M ( 'Product' );
		if ($this->isPost ()) {
			$start_id = isset ( $_POST ['start_id'] ) ? intval ( $_POST ['start_id'] ) : 1;
			$end_id = isset ( $_POST ['end_id'] ) ? intval ( $_POST ['end_id'] ) : 1;
			$allPro = $product->where ( 'statice_name != "" and id>=' . $start_id . ' and id<=' . $end_id )->select ();

			foreach ( $allPro as $v ) {
				$id = $v ['id'];
				if (! $id) {
					return;
				}

				if (empty ( $v ['statice_name'] )) {
					continue;
				}

				$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/product_description/id/' . $id );
				$g= file_get_contents ($this->root_name.__ROOT__.'/Index/guide/id/'.$id);
				$t= file_get_contents ($this->root_name.__ROOT__.'/Index/tech_spec/id/'.$id);
				$r= file_get_contents ($this->root_name.__ROOT__.'/Index/reviews/id/'.$id);
				if (file_put_contents ( "./" . $v ['statice_name'] . '.html', $p ) && file_put_contents ( "./" . $v ['statice_name'] . '_Guide.html', $g ) && file_put_contents ( "./" . $v ['statice_name'] . '_Tech_Spec.html', $t ) && file_put_contents ( "./" . $v ['statice_name'] . '_Reviews.html', $r )) {
					$product->where ( 'id=' . $id )->save ( array (
							'is_statice' => '1'
					) );
				} else {
					echo "<script>alert('静态化失败');history.back();</script>";
				}
			}
			echo "<script>alert('静态化成功');location.href='" . __URL__ . "/staticAllProduct';</script>";
		} else {
			$id = $product->field ( "id" )->limit ( 1 )->order ( 'id desc' )->find ();
			$this->assign ( "id", $id );
			$this->display ();
		}
	}

	// 生产网站地图
	public function sitemap() {
		$html = '<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="' . $this->site_root . '/sitemap.xsl"?>';
		$html .= '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
		$webUrl = $this->site_root . '/';
		$html .= $this->url ( $webUrl, date ( 'c', time () ), 'monthly', '1.0' );
		$article = M ( 'Article' );
		$product = M ( 'Product' );
		$adata = $article->where ( "statice_name != '' and is_statice=1" )->select ();
		$pdata = $product->where ( "statice_name != '' and is_statice=1" )->select ();
		foreach ( $adata as $k => $v ) {
			$html .= $this->url ( $webUrl . $v ['statice_name'] . ".html", date ( 'c', $v ['add_time'] ), 'monthly', '1' );
		}
		foreach ( $pdata as $k => $v ) {
			$html .= $this->url ( $webUrl . $v ['statice_name'] . ".html", date ( 'c', $v ['add_time'] ), 'monthly', '1' );
		}
		$html .= '</urlset>';
		file_put_contents ( 'sitemap.xml', $html );
		echo "<script>alert('生产网站地图成功');history.go(-1);</script>";
	}
	public function footlink() {
		$this->display ();
	}
	public function staticAll() {
		$this->display ();
	}
	public function url($loc, $lastmod, $changefreq, $priority) {
		$html = '<url>
	　　			  <loc>' . $loc . '</loc>
				　<lastmod>' . $lastmod . '</lastmod>
				　<changefreq>' . $changefreq . '</changefreq>
				　<priority>' . $priority . '</priority>
			　</url> ';
		return $html;
	}

	// 静态化搜索页面
	public function staticeSearchResult() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/search_result' );
		if (! file_put_contents ( 'search_result.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();;</script>";
	}

	// 静态化文章大类
	public function staticeAllTutorials() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$ac = M ( 'ArticleClassify' );
		$data = $ac->where ( "statice_name != ''" )->select ();

		foreach ( $data as $v ) {
			$id = $v ['id'];
			$pid = $v ['pid'];
			if (! $id) {
				return;
			}
			if (empty ( $v ['statice_name'] )) {
				continue;
			}
			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/howto/id/' . $id . '/pid/' . $pid );
			if (file_put_contents ( "./" . $v ['statice_name'] . '.html', $p )) {
				$ac->where ( 'id=' . $id )->save ( array (
						'is_statice' => '1'
				) );
			} else {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');location.href='" . __URL__ . "/article_classify_manage';</script>";
	}

	// 静态化购买页面
	public function staticeBuy() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$product = M ( 'Product' );
		$allPro = $product->where ( 'statice_name != ""' )->select ();

		foreach ( $allPro as $v ) {
			$id = $v ['id'];
			if (! $id) {
				return;
			}

			if (empty ( $v ['statice_name'] )) {
				continue;
			}

			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/buy/id/' . $id );
			if (! file_put_contents ( "./purchasing/" . $v ['statice_name'] . '.html', $p )) {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');history.back();;</script>";
	}

	// 静态化下载页面
	public function staticeDownload() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$product = M ( 'Product' );
		if ($this->isPost ()) {
			$start_id = isset ( $_POST ['start_id'] ) ? intval ( $_POST ['start_id'] ) : 1;
			$end_id = isset ( $_POST ['end_id'] ) ? intval ( $_POST ['end_id'] ) : 1;
			$allPro = $product->where ( 'statice_name != "" and id>=' . $start_id . ' and id<=' . $end_id )->select ();

			foreach ( $allPro as $v ) {
				$id = $v ['id'];
				$pid = $v ['pid'];
				if (! $id || ! $pid) {
					return;
				}

				if (empty ( $v ['statice_name'] )) {
					continue;
				}

				$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/download/id/' . $id . '/pid/' . $pid );
				if (! file_put_contents ( "./downloading/" . $v ['statice_name'] . '.html', $p )) {
					echo "<script>alert('静态化失败');history.back();</script>";
				}
			}
			echo "<script>alert('静态化成功');history.back();;</script>";
		} else {
			$id = $product->field ( "id" )->limit ( 1 )->order ( 'id desc' )->find ();
			$this->assign ( "id", $id );
			$this->display ();
		}
	}

	// 静态化staticePrivacyPolicy
	public function staticePrivacyPolicy() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/privacyPolicy' );
		if (! file_put_contents ( 'privacy-policy.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	// 静态化staticeTermsOfUse
	public function staticeTermsOfUse() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/termsOfUse' );
		if (! file_put_contents ( 'terms-of-use.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	// 静态化staticeAboutUs
	public function staticeAboutUs() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/aboutUs' );
		if (! file_put_contents ( 'about-us.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	
	// 静态化staticeContactUs
	public function staticeContactUs() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/contactUs' );
		if (! file_put_contents ( 'contact-us.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	//静态化staticeResources
	public function staticeResources() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/resources' );
		if (! file_put_contents ( 'resources.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}

	// 加上去的
	public function staticeAllProduct() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/allproduct' );
		if (! file_put_contents ( 'product.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	// 没有
	public function staticeTutorials() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/tutorials' );
		if (! file_put_contents ( 'tutorials.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	// 加上
	public function staticeAllDownload() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/alldownload' );
		if (! file_put_contents ( 'download.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}
	// 加上
	public function staticeSupport() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/support' );
		if (! file_put_contents ( 'support.html', $p )) {
			echo "<script>alert('静态化失败');history.back();</script>";
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}

	// 静态化全部栏目（二级）
	public function staticeAllPlate() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$plate = M ( 'Plate' );
		// $data=$plate->where('statice_name != ""')->select();
		$data = $plate->field ( 'think_plate.*,b.big_category_name  as bname' )->join ( 'left join think_bigcategory b on b.id=think_plate.pid' )->where ( 'think_plate.statice_name != ""' )->select ();
		foreach ( $data as $v ) {
			$id = $v ['id'];
			if (! $id) {
				return;
			}
			if (empty ( $v ['statice_name'] )) {
				continue;
			}
			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/classify/id/' . $id );
			if (file_put_contents ( "./" . $v ['bname'] . "/" . $v ['statice_name'] . '.html', $p )) {
				$plate->where ( 'id=' . $id )->save ( array (
						'is_statice' => '1'
				) );
			} else {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}

	// 静态化全部howto页
	public function staticeAllHowto() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$bigcategory = M ( 'Bigcategory' );
		$data = $bigcategory->where ( 'statice_name != ""' )->select ();
		foreach ( $data as $v ) {
			$id = $v ['id'];
			if (! $id) {
				return;
			}
			if (empty ( $v ['statice_name'] )) {
				continue;
			}
			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/howto/id/' . $id . '/type/select' );
			if (file_put_contents ( "./" . $v ['statice_name'] . '.html', $p )) {
				$bigcategory->where ( 'id=' . $id )->save ( array (
						'is_statice' => '1'
				) );
			} else {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');history.back();</script>";
	}

	// 静态化resources
	public function staticeAllResources() {
		header ( "Content-Type:text/html; charset=utf-8" );
		$ac = M ( 'ArticleClassify' );
		$data = $ac->where ( "statice_name != ''" )->select ();

		foreach ( $data as $v ) {
			$id = $v ['id'];
			if (! $id) {
				return;
			}
			if (empty ( $v ['statice_name'] )) {
				continue;
			}
			$p = file_get_contents ( $this->root_name . __ROOT__ . '/Index/resources/id/' . $id );
			if (file_put_contents ( "./" . $v ['statice_name'] . '.html', $p )) {
				$ac->where ( 'id=' . $id )->save ( array (
						'is_statice' => '1'
				) );
			} else {
				echo "<script>alert('静态化失败');history.back();</script>";
			}
		}
		echo "<script>alert('静态化成功');location.href='" . __URL__ . "/article_classify_manage';</script>";
	}
}